package com.diamond.util;

import java.text.DecimalFormat;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.diamond.entity.Diamond;
import com.diamond.entity.Formula;

public class DiamondUtil {
	/**
	 * 得到成本价
	 * @param bo
	 * @param supFormulas
	 * @return
	 */
	public static Double getMidPrice(Diamond bo,List supFormulas){
		if(bo.getSupPrice()==null||supFormulas==null||supFormulas.size()==0)return null;
		for(int k=0;k<supFormulas.size();k++){
			Formula supFormula=(Formula) supFormulas.get(k);
			if(DiamondUtil.suitFormula(supFormula, bo)){
				Double price=bo.getSupPrice()*supFormula.getRate();
				return price;
			}
		}
		return null;
	}
	/**
	 * 得到零售价
	 * @param bo
	 * @param supFormulas
	 * @return
	 */
	public static Double getBuyOrSpePrice(Diamond bo,List supFormulas,List formulas){
		if(bo.getSupPrice()==null||supFormulas==null||supFormulas.size()==0||formulas==null||formulas.size()==0)return null;
		for(int k=0;k<formulas.size();k++){
			Formula formula=(Formula) formulas.get(k);
			if(DiamondUtil.suitFormula(formula, bo)){
				Double supPrice=getMidPrice(bo,supFormulas);
				if(supPrice!=null){
					Double price = supPrice*formula.getRate();
					return price;
				}
			}
		}
		return null;
	}
	/**
	 * 匹配公式
	 * @param formula
	 * @param bo
	 * @return
	 */
	public static Boolean suitFormula(Formula formula,Diamond bo){
		if(formula==null||bo==null)return false;
		//比较重量范围
		String str=formula.getWeight();
		if(!StringUtils.isEmpty(str)){
			String[] strs=str.split("-");
			Double min=Double.parseDouble(strs[0]);
			Double max=Double.parseDouble(strs[1]);
			if (null != bo.getWeight() && !"".equals(bo.getWeight()) && 0 != bo.getWeight()) {
				Double weight=bo.getWeight();
				if(weight>max||weight<min){
					return false;
				}
			} 
		}
		//比较颜色范围
		if(!compara(bo.getColor(),formula.getColour())){
			return false;
		}
		//比较净度范围
		if(!compara(bo.getClarity(),formula.getPurity())){
			return false;
		}
		//比较切工范围
		if(!compara(bo.getCulet(),formula.getCutter())){
			return false;
		}
		//比较对称范围
		if(!compara(bo.getSymmetry(),formula.getSymmetry())){
			return false;
		}
		//比较抛光范围
		if(!compara(bo.getPolish(),formula.getBuffing())){
			return false;
		}
		//比较荧光范围
		if(!compara(bo.getFluor(),formula.getFluorescence())){
			return false;
		}
		//比较荧光范围
		if(!compara(bo.getClarity(),formula.getCerificate())){
			return false;
		}

		
		return true;
		
	}
	public static Boolean compara(String proper,String str){
		if(str==null||"".equals(str))return true;
		if(proper==null||"".equals(proper))return false;
		str=str.replace("|", ",");
		String[] strs=str.split(",");
		for(int i=0;i<strs.length;i++){
			if(proper.equals(strs[i])){return true;}
		}
		return false;
	}
	public static DecimalFormat   df=null;
	public static String DoubleFormat(Double in){
		if(df==null)df=new DecimalFormat("#");
		if(in==null||in==0)return "0";
		return  df.format(in);
	}
	public static DecimalFormat   df1=null;
	public static String WeightFormat(Double in){
		if(df1==null)df1=new DecimalFormat("0.00");
		if(in==null)return null;
		return  df1.format(in);
	}
	
	
	public static void main(String[] args){
		Double d1=6557456/3.9d;
//		DecimalFormat   df   =   new   DecimalFormat("#");
		System.out.println(d1);
		System.out.println(DiamondUtil.DoubleFormat(d1));
	}
}
